package rule_window

import (
	"file_explorer/common/util"
	"file_explorer/model"
	"file_explorer/view/packed_widgets"

	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/widget"
)

func (window *RuleWindow) newRuleDeleteTab(onSubmit func()) *container.TabItem {
	form := widget.NewForm()

	offsetEntry := packed_widgets.NewNumEntry(util.PtrInt(0), nil, nil)
	lengthEntry := packed_widgets.NewNumEntry(util.PtrInt(1), nil, nil)

	form.Append("*位置", offsetEntry.Container)
	form.Append("*长度", lengthEntry.Container)
	form.OnSubmit = func() {
		window.tabCtx.Rules = append(window.tabCtx.Rules, model.NewDeleteRule(offsetEntry.GetValue(0), lengthEntry.GetValue(0)))
		onSubmit()
	}

	validator := func() {
		if offsetEntry.IsEmpty() || lengthEntry.IsEmpty() {
			form.Disable()
			return
		}
		form.Enable()
	}
	validator()
	offsetEntry.OnChange = func(s string) {
		validator()
	}
	lengthEntry.OnChange = func(s string) {
		validator()
	}

	return container.NewTabItem("删除", form)
}
